package com.example.musicgui.view;
import com.example.musicgui.controller.UserController;
import com.example.musicgui.model.ResponseBodyMessage;
import com.example.musicgui.model.User;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.control.Alert.AlertType;
public class Login {
    private Stage stage;
    private Scene loginScene;
    private Page page;
    private Button loginButton;
    private Button signButton;
    private TextField usernameField;
    private PasswordField passwordField;
    private Text welcomeLabel; // 添加欢迎标签

    public Login(Stage stage) {
        this.stage = stage;
        welcomeLabel = new Text("欢迎来到声匠");
        welcomeLabel.setStyle(Styles.TEXT_ONE);


        VBox loginLayout = new VBox(10);
        loginLayout.setAlignment(Pos.CENTER);
        initializeFields();
        loginLayout.getChildren().addAll(welcomeLabel,usernameField, passwordField, loginButton, signButton);
        loginScene = new Scene(loginLayout, 400, 350);
    }
    public void show() {
        stage.setScene(loginScene);
        stage.show();
        stage.centerOnScreen();
    }

    public void setPage(Page page) {
        this.page = page;
    }

    private void initializeFields() {
        usernameField = new TextField();
        usernameField.setPromptText("Username");
        usernameField.setStyle(Styles.TEXTFIELD_ONE);

        passwordField = new PasswordField();
        passwordField.setPromptText("Password");
        passwordField.setStyle(Styles.TEXTFIELD_ONE);

        getButtons();
        buttonActions();
    }

    private void switchToIndex(Integer userId) {
        page.getIndex().show(userId);
    }

    private void switchToSign() {
        page.getSign().show();
    }

    private void getButtons() {
        this.loginButton = new Button("登录");
        loginButton.setStyle(Styles.BUTTON_ONE);
        this.signButton = new Button("没有账号，立即注册");
        signButton.setStyle(Styles.BUTTON_TWO);
    }

    private void buttonActions() {
        this.loginButton.setOnAction(event -> {

            String username = usernameField.getText();
            String password = passwordField.getText();
            User user = new User(username,password);
            UserController userController = new UserController();
            try {
                ResponseBodyMessage<String> loginMessage = userController.login(user);
                if (loginMessage.getStatus()){
                    Integer id = Integer.valueOf(loginMessage.getData());
                    switchToIndex(id);
                }else {
                    showError(loginMessage.getMessage());
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });

        this.signButton.setOnAction(event -> {
            switchToSign();
        });
    }

    private void showError(String message) {
        // 弹出错误信息
        Alert alert = new Alert(AlertType.ERROR);
        alert.setTitle("Error");
        alert.setHeaderText(null);
        alert.setContentText(message);
        alert.showAndWait();
    }
}